const jwt = require("jsonwebtoken");
module.exports = async (ctx, next) => {
  const token = ctx.headers.authorization?.replace('Bearer ', '');
  if (!token) {
    ctx.status = 401;
    ctx.body = {
      message: 'No authorization token'
    };
    return;
  }
  try {
    const decoded = jwt.verify(token, 'zshawk1982');
    ctx.state.user = decoded;
    await next();
  } catch (err) {
    ctx.status = 401;
    ctx.body = {
      message: 'Invalid token'
    };
    return;
  }
};